/**
 * 验证器
 * 
 */
function Validate() {
    // this.check = function (d, r, m = "model") {
    //     this.error = "";
    //     var field = [],
    //         rule = [],
    //         msg = [],
    //         data = d;
    //     /*遍历规则数据*/
    //     r.forEach(item => {
    //         field.push(item[0]);
    //         msg.push(item[1]);
    //         rule.push(item[2]);
    //     });/*循环验证字段*/
    //     for (var index in field) {
    //         var key = field[index];
    //         var flag = this.doValidate(data[key], rule[index], msg[index]);
    //         if (!flag) {
    //             var formDom = document.querySelector("[" + m + "='" + key + "']");
    //             if (formDom != null) formDom.focus();
    //             return false;
    //         }
    //     }
    //     return true;
    // }
    this.check = function (data, rule, model = "model") {
        for (var index in rule) {
            var item = rule[index];
            var doData = {
                value: data[item[0]],
                rule: item
            };
            if (item[3] != null) doData.confirmValue = data[item[3]];
            var flag = this.doValidate(doData);
            if (!flag) {
                var formDom = document.querySelector("[" + model + "='" + item[0] + "']");
                if (formDom != null) formDom.focus();
                return false;
            }
        }
        return true;
    }
    /**返回验证器错误 */
    this.getError = function () {
        return this.error;
    }
    /**开始验证 */
    this.doValidate = function (data) {
        flag = true;
        rule = data.rule;
        value = data.value;

        switch (rule[1]) {
            case 'require':
                if (value == "" || value == null) {
                    flag = false;
                    this.error = rule[2] == null ? "请填好完整表单数据" : rule[2] + "不能为空";
                }
                break;
            case 'phone':
                var reg = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
                if (!reg.test(value)) {
                    flag = false;
                    this.error = rule[2] + "不是有效手机号";
                }
                break;
            case 'email':
                var reg = /^[_a-z0-9-\.]+@([-a-z0-9]+\.)+[a-z]{2,}$/i;
                if (!reg.test(value)) {
                    flag = false;
                    this.error = rule[2] + "不是有效邮箱";
                }
                break;
            case 'chinese':
                var res = /[u4e00-u9fa5]/;
                if (!res.test(value)) {
                    flag = false;
                    this.error = rule[2];
                }
                break;
            case 'qq':
                var res = /[1-9][0-9]{4,}/;
                if (!res.test(value)) {
                    flag = false;
                    this.error = rule[2];
                }
                break;
            case 'ip':
                var res = /d+.d+.d+.d+/;
                if (!res.test(value)) {
                    flag = false;
                    this.error = rule[2];
                }
                break;
            case 'confirm':
                if (value != data.confirmValue) {
                    flag = false;
                    this.error = rule[2] == '' ? "两次输入不一致" : rule[2];
                }
                break;
        }
        return flag;
    }
}
